<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :okButtonProps="{ props: {disabled: disableSubmit} }"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="商品属性分类id"
          :hidden="hiding"
        >
          <a-input placeholder="请输入商品属性分类id"
                   v-decorator="['productAttributeCategoryId', {'initialValue':this.mainId}]"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="名称"
        >
          <a-input placeholder="请输入名称" v-decorator="['name', {}]"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="分类筛选样式"
          :hidden="hiding"
        >
          <j-dict-select-tag v-decorator="['filterType', {}]" placeholder="筛选样式" :type="'radio'" :triggerChange="true"
                             dictCode="filter_type"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="能否进行检索"
          :hidden="hiding"
        >
          <j-dict-select-tag v-decorator="['searchType', {}]" placeholder="检索类型" :type="'radio'" :triggerChange="true"
                             dictCode="search_type"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="商品属性关联"
          :hidden="hiding"
        >
          <j-dict-select-tag v-decorator="['relatedStatus', {}]" placeholder="属性关联" :type="'radio'"
                             :triggerChange="true" dictCode="yn"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="可选类型"
        >
          <j-dict-select-tag v-decorator="['selectType', {}]" placeholder="可选类型" :type="'radio'" :triggerChange="true"
                             dictCode="select_type"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="录入方式"
        >
          <j-dict-select-tag v-decorator="['inputType', {}]" placeholder="录入方式" :type="'radio'" :triggerChange="true"
                             @change="handleInputType" dictCode="input_type"/>
        </a-form-item>

<!--        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="可选值列表"
        >
          <a-input placeholder="请输入可选值列表，以逗号隔开" v-decorator="['inputList', {}]"/>
        </a-form-item>-->

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          :hidden="model.inputType !== '1'"
          label="可选值列表">
          <div>
            <a-form-item layout="inline" v-for="(detail,detailIndex) in detailOptions">
              <a-icon type="minus-circle" v-if="detailOptions.length > 1" @click="handleDelDetailClick(detailIndex)"/>
              <span>&nbsp;&nbsp;</span>
              <a-input v-model="detail.value" style="width: 60%" :placeholder="'选项'+(detailIndex+1)"/>
            </a-form-item>
            <span @click="handleAddDetailClick">
              <a-icon type="plus-circle" theme="filled"/>&nbsp;&nbsp;添加选项
            </span>
          </div>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="是否支持手动新增"
          :hidden="hiding"
        >
          <j-dict-select-tag v-decorator="['handAddStatus', {}]" placeholder="手动新增" :type="'radio'"
                             :triggerChange="true" dictCode="yn"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="排序"
        >
          <a-input-number v-decorator="[ 'sort', {}]"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="属性的类型；0->规格；1->参数"
          :hidden="hiding"
        >
          <a-input-number v-decorator="[ 'type', {'initialValue':this.type}]"/>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { httpAction } from '@/api/manage'
import pick from 'lodash.pick'
import moment from 'moment'
import JDate from '@/components/jeecg/JDate'

export default {
  components: {
    JDate
  },
  name: 'PmsProductAttributeModal',
  data() {
    return {
      title: '操作',
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      moment,
      format: 'YYYY-MM-DD HH:mm:ss',
      disableSubmit: false,
      mainId: '',
      type: '',
      detailOptions: [],
      hiding: false,
      confirmLoading: false,
      form: this.$form.createForm(this),
      validatorRules: {},
      url: {
        add: '/pms/pmsProductAttributeCategory/addPmsProductAttribute',
        edit: '/pms/pmsProductAttributeCategory/editPmsProductAttribute'
      }
    }
  },
  created() {
  },
  methods: {
    add(mainId, type) {
      if (mainId) {
        this.edit({ 'mainId': mainId, 'type': type }, '')
      } else {
        this.$message.warning('请选择一条数据')
      }
    },
    detail(record) {
      this.edit(record, 'd')
    },
    edit(record, v) {
      if (v == 'e') {
        this.hiding = false
        this.disableSubmit = false
      } else if (v == 'd') {
        this.hiding = false
        this.disableSubmit = true
      } else {
        this.hiding = true
        this.disableSubmit = false
      }
      this.detailOptions = []
      this.form.resetFields()
      this.mainId = record.mainId
      this.type = record.type
      this.model = Object.assign({}, record)

      if (this.model.inputList !== null && this.model.inputList !== '' && this.model.inputList !== undefined) {
        this.detailOptions = this.model.inputList.split(',').map(value => ({value}))
      } else {
        this.detailOptions.push({value:""})
      }


      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.model, 'productAttributeCategoryId', 'name', 'selectType', 'inputType', 'sort', 'filterType', 'searchType', 'relatedStatus', 'handAddStatus', 'type'))
        // 时间格式化
      })
    },
    handleInputType(e) {
      this.model.inputType = e
    },
    handleAddDetailClick(e) {
      this.detailOptions.push({value:""})
    },
    handleDelDetailClick(e) {
      this.detailOptions.splice(e, 1)
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          let formData = Object.assign(this.model, values)
          if (formData.inputType === '1') {
            formData.inputList = this.detailOptions.map(x => x.value).join(',')
          } else {
            formData.inputList = '';
          }
          //时间格式化
          httpAction(httpurl, formData, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
            that.close()
          })
        }
      })
    },
    handleCancel() {
      this.close()
    }
  }
}
</script>

<style scoped>

</style>
